package com.google.code.head2head.gwtmodules.client.fileupload;


import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.QuickTips;
import com.gwtext.client.widgets.event.ButtonListener;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormConfig;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.TextFieldConfig;
import com.gwtext.client.widgets.form.event.FormListener;

public class FileUploadPanel extends VerticalPanel {

	private Form fileForm;

	private  TextField file1Field = null;

	private  TextField file2Field = null;
	
	public FileUploadPanel() {
		initWidget();
	}

	private void initWidget() {

		add(new HTML("</br>1) Choose files to upload on your computer"));
		Field.setMsgTarget("side");
		QuickTips.init(); 
		fileForm = new Form(new FormConfig() {
			{
				setUrl("./fileUploadServlet");
				setFileUpload(true);
				setHeader("File Upload Form");
				setSurroundWithBox(true);
				setFormListener(new FormListener(){

					public boolean doBeforeAction(Form form) {
						// TODO Auto-generated method stub
						return true;
					}

					public void onActionComplete(Form form) {
						
						clear();
						add(new EntryInfoPanel());
						MessageBox.alert("Message","Entry was created");
					}

					public void onActionFailed(Form form) {
						MessageBox.alert("Message","There was a problem during form submission");
						
					}

					public void onClientValidation(Form form, boolean valid) {
						// TODO Auto-generated method stub
						
					}
					
				});
				
				
			}
			
		});

		
		file1Field = new TextField(new TextFieldConfig() {
			{
				setFieldLabel("Image 1");
				setName("image1");
				setInputType("file");
				setAllowBlank(true);
			}
		});
		fileForm.add(file1Field);

		file2Field = new TextField(new TextFieldConfig() {
			{
				setFieldLabel("Image 2");
				setName("image2");
				setInputType("file");
				setAllowBlank(true);
			}
		});
		fileForm.add(file2Field);

		Button submitButton = new Button("submit");
		submitButton.addButtonListener(new ButtonListener() {

			public void onClick(Button button, EventObject e) {
				if (!file1Field.getText().toLowerCase()
						.endsWith(".jpg")
						|| !file2Field.getText().toLowerCase()
								.endsWith(".jpg")) {
					MessageBox.alert("Message",
							"Please specify a jpg file ending with '.jpg'.");
					e.stopEvent();
				}
				fileForm.submit();
				
			}

			public void onMouseOut(Button button, EventObject e) {
				// TODO Auto-generated method stub

			}

			public void onMouseOver(Button button, EventObject e) {
				// TODO Auto-generated method stub

			}

			public void onToggle(Button button, boolean pressed) {
				// TODO Auto-generated method stub

			}

		});
		fileForm.addButton(submitButton);
		fileForm.render();
		this.add(fileForm);

	}
}
